import {
	 getquery, add, isdelete, getguid, getqueryall
} from '@/api/ZL_Equipmentmanagement'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import router, {
  resetRouter
} from '@/router'
import store from '@/store'
import { status } from 'nprogress'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  menus: []
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_MENU: (state, menus) => {
    state.menus = menus
  }
}

const actions = {
  // user login
  add({
    commit
  }, userInfo) {
    const {
      name,
      model,
      statusname,
      status,
      workshop,
      manufacturer,
      location,
      purchaseDate,
      imagepath,
      id,
      Status,
      EquipmentTypesManagementId,
      code,
      equipmenttypesmanagementname,
      workshopmanagementname,
      workshopmanagementid
    } = userInfo
    return new Promise((resolve, reject) => {
      add({
        name,
        model,
        statusname,
        status,
        workshop,
        manufacturer,
        location,
        purchaseDate,
        imagepath,
        id,
        Status,
        EquipmentTypesManagementId,
        code,
        equipmenttypesmanagementname,
        workshopmanagementname,
        workshopmanagementid
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getquery({
    commit
  }, userInfo) {
    const {
      page,
      limit,
      name
    } = userInfo
    return new Promise((resolve, reject) => {
      getquery({
        limit: limit,
        page: page,
        name: name
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getqueryall({
    commit
  }, userInfo) {
    const {
      page,
      limit
    } = userInfo
    return new Promise((resolve, reject) => {
      getqueryall({
        limit: limit,
        page: page
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  Cityquery({
    commit
  }, userInfo) {
    const {

    } = userInfo
    return new Promise((resolve, reject) => {
      Cityquery({

      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  isdelete({
    commit
  }, userInfo) {
    const {
      id
    } = userInfo
    return new Promise((resolve, reject) => {
      isdelete({
        id
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getguid({
    commit
  }, userInfo) {
    const {
      id
    } = userInfo
    return new Promise((resolve, reject) => {
      getguid({
        id
      }).then(response => {
        const {
          data
        } = response
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  async changeRoles({
    commit,
    dispatch
  }, role) {
    const token = role + '-token'

    commit('SET_TOKEN', token)
    setToken(token)

    const {
      roles
    } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', roles, {
      root: true
    })
    // dynamically add accessible routes
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, {
      root: true
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
